package cn.ecut.controller;

import cn.ecut.model.Repair;
import cn.ecut.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@Controller
@SessionAttributes("repair")
public class RepairController {
    @Autowired
    RepairService repairService;

    @RequestMapping(value = "/insertRepair")
    public String insertRepair(@RequestParam("userId") String userId,
                               @RequestParam("repair_project") String repair_project,
                               @RequestParam("housing_estate") String housing_estate,
                               @RequestParam("address") String address,
                               @RequestParam("phone") String phone,
                               @RequestParam("request_people") String request_people,
                               @RequestParam("description") String description) {
        Repair repair = new Repair();
        //维修单据编号，随机生成
        String id = String.valueOf(100000 + new Random().nextInt(899999));
        repair.setId(Integer.parseInt(id));
        repair.setUserId(userId);
        repair.setRepair_project(repair_project);
        repair.setHousing_estate(housing_estate);
        repair.setAddress(address);
        repair.setPhone(phone);
        repair.setRequest_people(request_people);
        repair.setDescription(description);
        repair.setDeductionStatus(0);
        repairService.insertRepair(repair);
        return "user/insertRepairSuccess";

    }
    @RequestMapping("/getRepairInfo")
    public String getRepairInfo(@RequestParam("userId") String userId,
                                HttpServletRequest request,
                                Model model){
        request.getSession().removeAttribute("repair");
        Repair repair = repairService.getRepairInfo(userId);
        model.addAttribute("repair",repair);
        return "user/Settlement_center";
    }

    @RequestMapping("/updateReapir")
    public String updateReapir(@RequestParam("id") String id,
                               @RequestParam("maintenanceCosts") String maintenanceCosts){
        Repair repair = new Repair();
        repair.setId(Integer.parseInt(id));
        repair.setMaintenanceCosts(Double.parseDouble(maintenanceCosts));
        repairService.updateReapir(repair);
        return "admin/getRepairList";
    }
}
